<?php
/**
 *  $Id:$
 * 
 *  Swift Mailer
 * 
 * @author  nuarharuha
 * @since 1.0  
 */


if (!isset($_REQUEST['req'])) header('Location: ../');

unset($_REQUEST['req']);

/**
 *  set default value
 *  error
 */
$rep_client = array('name'=>'Anon',
    'email'=>'info+anon@heatstress.com.my',
    'tel'=>'no phone',
    'message'=>'no msg',
    'company'=>'no company name');

    foreach ($rep_client as $k=>$v){
        
        if (!array_key_exists($k, $_REQUEST)) {
            $_REQUEST[$k] = $v;
        }
    }

define('DSEP',DIRECTORY_SEPARATOR);

define('SWIFTPATH',dirname(__FILE__).DSEP.'swift'.DSEP );

define('F_MESSAGE',"Name: '%s'\r\nEmail: '%s'\r\nTel: %s".PHP_EOL.'Company: %s'.PHP_EOL);

/**
 *  parse configuration settings
 *  from config.ini
 */
$config =  parse_ini_file('config.ini', true);

$csmtp = (object) $config['smtp'];

/**
 *  main swift autoloader class
 */
require_once SWIFTPATH.'swift_required.php';

$message = Swift_Message::newInstance();

$client = (object) $_REQUEST;
$header_msg = sprintf(F_MESSAGE, $client->name, $client->email, $client->tel, $client->company);
$header_msg .= str_repeat('__',40).PHP_EOL.PHP_EOL;

$message->setSubject($config['mail']['subject'])
    ->setTo($csmtp->emailto)
    ->setReturnPath($csmtp->bounce)
    ->setFrom(array($client->email => $client->name))
    ->setBody($header_msg.$client->message);

/**
 *  message id
 */
 
$msgId = $message->getHeaders()->get('Message-ID');
$msgId->setId(time() . '.' . uniqid('habastress+') . $csmtp->mid); 


// Create the Transport
$transport = Swift_SmtpTransport::newInstance($csmtp->uri, $csmtp->port,'tls')
  ->setUsername($csmtp->user)
  ->setPassword($csmtp->pwd);
  
$mailer = Swift_Mailer::newInstance($transport);

if ($mailer->send($message,$failures))
{
  header("Location: ../contact-submitted.html");
}
else
{
  header("Location: ../contact-failed.html");
  //var_dump($failures);
}
?>